﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ViewModel;
using DataLayer;
using System.Net;

namespace Service
{
    public class Login
    {
        public static Member ValidateUserLogin(string username, string password)
        {
            Member obj = new Member();
            try
            {
                using (iTrackerEntities DB = new iTrackerEntities())
                {
                    obj = (from u in DB.users
                           join s in DB.shops on u.shop_id equals s.id
                           where u.username == username && u.password == password
                           && !u.is_deleted && !s.is_deleted
                           select new Member()
                           {
                               ID = u.id,
                               UserName = u.username,
                               Password = u.password,
                               Shop = new Shop()
                               {
                                   ID = s.id,
                                   Name = s.name
                               },
                               Role = (Role)u.role_id
                           }).FirstOrDefault();
                    //keep record on active session table
                    if (obj != null && obj.ID > 0)
                    {
                        activesession session = DB.activesessions.Where(x => x.user_id == obj.ID && x.is_active).FirstOrDefault();
                        IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
                        if (session != null && session.id > 0)
                        {
                            session.user_id = obj.ID;
                            session.time_in = DateTime.Now;
                            session.is_active = true;
                            session.machine_ip = ipHostInfo.AddressList[0].ToString();
                            session.time_out = null;
                        }
                        else
                        {
                            session = new activesession();
                            session.user_id = obj.ID;
                            session.time_in = DateTime.Now;
                            session.is_active = true;
                            session.machine_ip = ipHostInfo.AddressList[0].ToString();
                            session.time_out = null;
                            DB.activesessions.Add(session);
                        }
                        DB.SaveChanges();
                    }

                    //get announcer IP address
                    var settings = DB.appsettings.ToList();
                    ActiveSession.AnnouncerIP = settings.Where(x => x.name == "announcer_ip").FirstOrDefault().value;
                    ActiveSession.Port = Convert.ToInt32(settings.Where(x => x.name == "port").FirstOrDefault().value);
                }
            }
            catch (Exception ex)
            {
                Helper.ErrorLogger.WriteLog("ValidateUserLogin", ex);
                throw;
            }            
            return obj;
        }

        public static void LogOut(int userId)
        {
            try
            {
                using (iTrackerEntities DB = new iTrackerEntities())
                {
                    List<activesession> sessions = DB.activesessions.Where(x => x.user_id == userId && x.is_active).ToList();
                    foreach (var item in sessions)
                    {
                        item.is_active = false;
                        item.time_out = DateTime.Now;
                    }
                    DB.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Helper.ErrorLogger.WriteLog("LogOut", ex);
                throw;
            }
            
        }
    }
}
